package alpha.com.google.code.mechanic.janitor.ui;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import alpha.com.google.code.mechanic.janitor.controller.ContextData;
import alpha.com.google.code.mechanic.janitor.model.DefaultSysFilePath;
import alpha.com.google.code.mechanic.janitor.model.ProjectContext;

/**
 * The CreateContextDialog is to let the user create a context with a user
 * defined name under a default directory. The file folder name is set to
 * the same as the Context name.
 * 
 * Last_edited: Nov 6, 2014
 * 
 * @History
 * 1. Add code to check Context name duplication.
 * @author jingjiejiang
 * @edited Nov 6, 2014
 * 1) add a private field: ContextData ctxData
 * 2) add checking code in the method: modifyText(ModifyEvent e)
 *
 * @author jingjiejiang
 * @since Apr 11, 2014
 *
 */
public class CreateContextDialog extends Dialog {
  
  private Text textContextName;
  private Text textFilesLocation;
  private final ProjectContext projContext = new ProjectContext();
  private ContextData ctxData;
  
  private DefaultSysFilePath sysDefFilePath = new DefaultSysFilePath();

  /**
   * Create the dialog.
   * @param parentShell
   */
  public CreateContextDialog(Shell parentShell) {
    super(parentShell);
    
    ctxData = new ContextData();
    try {
      ctxData.readContextsFile();
    } catch (Exception e) {
      MessageDialog.openError(getParentShell(), "Couldn't read the "
          + "context.con file.", e.getMessage());
    }
  }
  
  @Override
  public void create() {
    super.create();
    getButton(IDialogConstants.OK_ID).setEnabled(false);
  }
  
  /**
   * Initialize all the Context files location at the home directory.
   */
  private void initFilesLocation () {
    
    String homedir = sysDefFilePath.getHomeDir();
    textFilesLocation.setText(homedir);
  }
  
  /**
   * Create contents of the dialog.
   * @param parent
   */
  @Override
  protected Control createDialogArea(Composite parent) {
    Composite container = (Composite) super.createDialogArea(parent);
    GridLayout gridLayout = (GridLayout) container.getLayout();
    gridLayout.numColumns = 2;
    
    Label lblContextName = new Label(container, SWT.NONE);
    lblContextName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    lblContextName.setText("Context Name:");
    
    textContextName = new Text(container, SWT.BORDER);
    textContextName.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        
        String name = textContextName.getText();
        boolean isenable = false;
        
        if(!name.equals("")) {
          if (ctxData.getContextsCollector().isValidCtxName(name)) {
            isenable = true;
          } else {
            MessageDialog.openError(getParentShell(), "Error", "There "
                + "already exist a Context with the same name. Please "
                + "specify another name for the new Context."); 
          }
        }
      
        getButton(IDialogConstants.OK_ID).setEnabled(isenable);
        
//        String fileloc = sysDefFilePath.getDefaultSysFilePath();
//        fileloc = fileloc + textContextName.getText();
        
        sysDefFilePath.initCtxFileDir();
        textFilesLocation.setText(sysDefFilePath.getDefaultSysFilePath()
            + textContextName.getText());
      }  
    });
    textContextName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    
    Label lblFileLocation = new Label(container, SWT.NONE);
    lblFileLocation.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    lblFileLocation.setText("Files Location:");
    
    textFilesLocation = new Text(container, SWT.BORDER);
    textFilesLocation.setEditable(false);
    GridData gd_textFilesLocation = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
    gd_textFilesLocation.widthHint = 270;
    textFilesLocation.setLayoutData(gd_textFilesLocation);
    textFilesLocation.setBackground(new Color(null, 204, 205, 207));
    
    initFilesLocation();

    return container;
  }

  /**
   * Create contents of the button bar.
   * @param parent
   */
  @Override
  protected void createButtonsForButtonBar(Composite parent) {
    createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
        true);
    createButton(parent, IDialogConstants.CANCEL_ID,
        IDialogConstants.CANCEL_LABEL, false);
    getButton(IDialogConstants.OK_ID).setEnabled(false);
  }

  /**
   * Return the initial size of the dialog.
   */
  @Override
  protected Point getInitialSize() {
    return new Point(450, 150);
  }
  
  /**
   * Save the user defined context's name and directory
   */
  @Override
  protected void okPressed() {
    
    projContext.setName(textContextName.getText());
    projContext.setFileLocation(textFilesLocation.getText());
    
    super.okPressed();
  }
  
  /**
   * Return the object of the ProjectContext.
   * @return
   */
  public ProjectContext getProjContext() {
    
    return this.projContext;
  }

}
